test-runner.ts ➔ suiteName   A
last analyzed

Complexity

Conditions 1

Size

Total Lines 5
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 5
dl 0
loc 5
c 0
b 0
f 0
rs 10
cc 1
1
import {readFileSync} from 'fs'
2
import type {Options} from './src/options.types'
3
import postcss7 = require("postcss")
4
import creator7 = require("./src/7")
5
import postcss8 from 'postcss8'
6
import creator8 = require("./src")
7
import type Processor from 'postcss8/lib/processor'
8
9
export type RunOpts = Partial<{
10
  from: string
11
  input: string
12
  outputPath: string | false
13
  errorsCount: number
14
}>
15
16
const {parse: $parse} = JSON
17
, launch = (opts?: Options) => {
18
  const launchers = [postcss7([creator7(opts)]), postcss8([creator8(opts)])]
19
20
  return (runOpts: RunOpts) => run(launchers, runOpts)
21
}
22
23
export default launch
24
25
export {
26
  rfs, rfsl, readOpts, suiteName
27
}
28
29
async function run(launchers: (postcss7.Processor | Processor)[], runOpts: RunOpts) {
30
  const {
31
    errorsCount = 0,
32
    from,
33
    input = from && rfs(from)
34
  } = runOpts
35
36
  if (!input)
37
    throw new Error("no test input")
38
39
  for (let i = 0; i < launchers.length; i++) {
40
    const result = await launchers[i].process(input, {from})
41
    , {outputPath: output} = runOpts
42
43
    expect(result.warnings()).toHaveLength(errorsCount)
44
45
    output && expect(rfsl(`${from}.d.ts`)).toStrictEqual(rfsl(output))
46
  }
47
}
48
49
function rfs(path: string) {
50
  return readFileSync(path).toString()
51
}
52
53
function rfsl(path: string, eol = "\n") {
54
  return rfs(path).split(eol)
55
}
56
57
function readOpts(path: string) {
58
  return $parse(rfs(path)) as Options
59
}
60
61
function suiteName(path: string) {
62
  return path
63
  .replace(/^.*[\/\\]/, '')
64
  .replace(/\..*$/, '')
65
}
66